package com.block.datapush.common.filter.impl;

import cn.hutool.core.util.StrUtil;
import com.block.common.service.LogFactory;
import com.block.common.util.BigDecimalUtil;
import com.block.datapush.common.enums.FilterErrorEnum;
import com.block.datapush.common.filter.IFilterHandlerService;
import com.block.datapush.common.model.param.FilterHandlerParam;
import com.block.datapush.common.model.result.FilterHandlerResult;
import com.block.db.entity.MemberExtInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 余额判断
 */
@Service
public class FilterPriceAmountHandlerService implements IFilterHandlerService {

    private static Logger log = LogFactory.getPushLog();

    private static List<String> successList = Arrays.asList("FORM", "SPREAD");

    @Override
    public FilterHandlerResult handlerFilterLogic(FilterHandlerParam handlerParam, MemberExtInfo memberExtInfo) {

        if (successList.contains(handlerParam.getOriginType())){
            // 表单不判断金额
            return FilterHandlerResult.success();
        }

        if (BigDecimalUtil.compare(handlerParam.getPriceAmount(), handlerParam.getRemainAmount()) <= 0){
            // 任务价格比 余额大，则通过
            return FilterHandlerResult.success();
        }

        String message = StrUtil.format("【priceAmount】{}【remainAmount】{}", handlerParam.getPriceAmount(), handlerParam.getRemainAmount());
        log.info("【机构条件判断】false【账户余额】{}", message);
        return FilterHandlerResult.error(FilterErrorEnum.PRICE_ERROR, message);
    }
}
